package com.example.DownloadImageHelperLibrary;

import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.BaseAdapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * Created by PNB on 3/28/14.
 */
public class NotifyAdapterDownloadImageTask  extends AsyncTask<String,Void,Void> {
    BaseAdapter baseAdapter;
    File path;
    String name;

    long size=0;
    long current_size=0;

    public NotifyAdapterDownloadImageTask(BaseAdapter baseAdapter, File folder, String name){
        this.path = folder;
        this.name = name;
        this.baseAdapter = baseAdapter;
    }

    public void download(String url){
        final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        final HttpGet getRequest = new HttpGet(url);

        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                return;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                path = new File(this.path, name);
                FileOutputStream fileOutputStream = new FileOutputStream(path);
                try {
                    size= entity.getContentLength();
                    inputStream = entity.getContent();
                    byte[] buffer = new byte[1024];
                    int bufferLength = 0;

                    while ((bufferLength = inputStream.read(buffer)) > 0){
                        fileOutputStream.write(buffer,0,bufferLength);
                        current_size+=bufferLength;
                        Log.i("Downloading "+name, ": " + current_size);

                    }
                    fileOutputStream.close();
                    return;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            getRequest.abort();
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }

    @Override
    protected Void doInBackground(String... params) {
        //android.os.Debug.waitForDebugger();
        download(params[0]);
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        //android.os.Debug.waitForDebugger();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        baseAdapter.notifyDataSetChanged();
    }
}
